$(document).ready(function () {
    query();
    queryPartys();
    selectstuApplyCondition();
});

/*教师无条件查询全部入党申请*/
var query = function () {
    //alert("-----")
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.stuApplyList",
        "sAjaxSource": "http://localhost:8080/partyApply/queryAllStuApplys",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search！');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>"
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumns": [
            {"mDataProp": "stuApplyId"},
            {"mDataProp": "stuInfoId"},
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp": "applyDate"},
            {"mDataProp": "partyOrganizationNum"},
            {
                "mDataProp": "applyPartyState",
                "mRender": function (data) {
                    if (data == 2) {
                        return "<span  class='badge badge-danger mr10 mb10'>已拒绝</span>";
                    }
                    if (data == 1) {
                        return "<span  class='badge badge-info mr10 mb10'>已同意</span>";
                    }
                    if (data == 0) {
                        return "<a href='#'><span  class='label label-danger mr10 mb10' onclick=\"reloadPage()\" id='agree'>同意</span></a>"
                            + "<a href='#'><span class='label label-success mr10 mb10' onclick=\"reloadPage()\"  id='refuse'>拒绝</span></a>";
                    }
                }
            }
        ],
    })
};

//无条件查询所有党组织
var queryPartys = function () {
    /*$("select[name=sel]").empty();  */   //清空seletct的数据
    $.ajax({
        url: "http://localhost:8080/partyApply/queryPartys",
        type: "GET",
        //dataType: "jsonp",//跨域问题
        dataType: "json",
        success: function (data) {
            // layer.alert("查询成功！");
            console.log(data);
            //向select中append、option标签
            //var optionString = "<option grade=\'请选择一级属性\' selected = \'selected\'>--请选择一级属性--</option>";
            var optionString = "";
            var beanList = data;            //返回的json数据
            for (x in data) { //遍历，动态赋值
                /*  "<option value=\"\"  +  +  \"\">" + +"</option>"
                  "  <option value=\"1\"  "+        " >"+华信软件学院直属党支部+"</option>"*/
                //"    <option  value=\"    " + data[x].partyOrganizationNum + "  \"   "
                optionString += "<option  value=\"" + data[x].partyOrganizationNum + "\"";//<option  value=\"1\">
                optionString += ">" + data[x].partyName + "</option>";  //动态添加数据
            }
            $("#party").append(optionString);  // 为当前name为sel的select添加数据。

        },
        error: function () {
            // console.log(34235)
            layer.alert("你的网络太慢了！查询失败!再试一次吧！");
        }
    });
}

//老师动态条件查询入党申请记录
function selectstuApplyCondition() {
    $(document).on('click', '#selectpartyApply', function () {
        var StuNum = $("#StuNum").val();
        var partyOrganizationNum = $("#party option:selected").val();
        //   var courseType = $("#courseType option:selected").text();
        var stuName = $("#stuName").val();
        var url = "http://localhost:8080/partyApply/querystuApplyConditions?StuNum=" + StuNum + "&stuName=" + stuName
            + "&partyOrganizationNum=" + partyOrganizationNum;
        var table = $('#dataTable').dataTable()
        if (table) {
            table.fnDestroy();
        }
        $('#dataTable').dataTable({
            "processing": true,
            "serverSide": true,
            "bInfo": false,//是否显示是否启用底边信息栏
            "sAjaxDataProp": "data.StuApplyList",
            "sAjaxSource": url,
            "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
            "fnPreDrawCallback": function (oSettings) {
                $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                $('.dataTables_length select').addClass('form-control input-small');
            },
            "oLanguage": {
                "sSearch": "",
                "sLengthMenu": "<span>_MENU_ entries</span>",
                "sZeroRecords": "暂无相关信息",
            },
            "bJQueryUI": false,
            'bAutoWidth': false,
            "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
            "aoColumns": [
                {"mDataProp": "stuApplyId"},
                {"mDataProp": "stuInfoId"},
                {"mDataProp": "stuNum"},
                {"mDataProp": "stuName"},
                {"mDataProp": "applyDate"},
                {"mDataProp": "partyOrganizationNum"},
                {
                    "mDataProp": "applyPartyState",
                    "mRender": function (data) {
                        if (data == 2) {
                            return "<span  class='badge badge-danger mr10 mb10'>已拒绝</span>";
                        }
                        if (data == 1) {
                            return "<span  class='badge badge-info mr10 mb10'>已同意</span>";
                        }
                        if (data == 0) {
                            return "<a href='#'><span  class='label label-danger mr10 mb10' onclick=\"reloadPage()\" id='agree'>同意</span></a>"
                                + "<a href='#'><span class='label label-success mr10 mb10'  onclick=\"reloadPage()\" id='refuse'>拒绝</span></a>";
                        }
                    }
                }
            ],
        })

    });
}

//老师同意学生入党申请
$(document).on('click', '#agree', function () {
    //获取学生申请记录的编号ID:$stuApplyId
    var $stuApplyId = $(this).parents("tr").children(0).eq(0).text();
    // var $stuApplyId = $(this).parents("tr").children(0).eq(0);
    alert("-----" + $stuApplyId);
    $.ajax({
        url: "http://localhost:8080/partyApply/agreeStuApply",
        type: "GET",
        // async: true,
        data: "stuApplyId=" + $stuApplyId,
        dataType: "json",
        success: function () {
            //query();//问题：不能自动刷新
            layer.alert("操作成功!");
        },
        error: function () {
            layer.alert("操作失败!");
        }
    });
});

//点击同意或拒绝的同时刷新页面
function reloadPage() {
    window.location.reload();
}

$(document).on('click', '#refuse', function () {
    //获取学生申请记录的编号ID:$stuApplyId
    var $stuApplyId = $(this).parents("tr").children(0).eq(0).text();
    $.ajax({
        url: "http://localhost:8080/partyApply/refuseApplyParty",
        type: "GET",
        // async: true,
        data: "stuApplyId=" + $stuApplyId,
        dataType: "json",
        success: function () {
            //query();
            layer.alert("操作成功!");
        },
        error: function () {
            layer.alert("操作失败--!");
        }
    });
});






